
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>注册</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="keywords" content="医药小伙伴">
  <meta name="description" content="医药小伙伴">
  <link rel="stylesheet" href="/static/css/layui.css">
  <link rel="stylesheet" href="/static/css/global.css">
  <script src="/static/js/jquery-1.11.3.min.js"></script>

</head>
<body>
<div class="main layui-clear">

  <div class="fly-panel fly-panel-user" pad20>
    <div class="layui-tab layui-tab-brief">
      <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
        <div class="layui-tab-item layui-show">
          <div class="layui-form layui-form-pane">
            <form method="post" id='reg'>
              <div class="layui-form-item">
                <label for="L_mobile" class="layui-form-label">手机号</label>
                <div class="layui-input-inline">
                  <input type="text" id='phone' name="phone" required lay-verify="required" class="layui-input"/>
                </div>
              </div>
              <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">验证码</label>
                <div class="layui-input-inline">
                  <input type="text"  id = 'code' name="code"  autocomplete="off" class="layui-input">
                </div>
              </div>

              <div class="layui-form-item">
                <button class="layui-btn layui-btn-small sendCode" >发送验证码</button>
                <button class="layui-btn layui-btn-small reg" >注册</button>

              </div>

            </form>
          </div>
        </div>
      </div>
    </div>
  </div>

</div>
<script >

  function check_phone(){
      var phoneReg = /^1[34578]\d{9}$/;
      var phone = $('#phone').val();
      if(phone ==''){
        alert('手机号码不能为空');
        return false;
      }
      if(phoneReg.test(phone) == false){
        alert("请填写正确的手机号码");
        return false;
      }
     //
  }
  $('.sendCode').click(function(e) {
      e.preventDefault();
      //console.log(check_phone());
      if(check_phone() === false){
        return false;
      }
      $(this).addClass('layui-btn-disabled');
      $(this).attr('disabled',"true");
        var timer="";
        var nums=60;
        var validCode=true;//定义该变量是为了处理后面的重复点击事件
        var code=$(this);
            if(validCode){
                validCode=false;
                timer=setInterval(function(){
                    if(nums>0){
                        nums--;
                        code.html(nums+"秒后重新发送");
                        //code.addClass("gray-bg");
                    }
                    else{
                        clearInterval(timer);
                        nums=60;//重置回去
                        validCode=true;
                        $(this).attr('disabled',"false");
                        code.removeClass("layui-btn-disabled");
                        code.html("发送验证码");
            }
                },1000)
            }
      $.post("{:url('code')}",{phone:$('#phone').val()},function(data){
        //console.log(data);
        if(data.status == 1){
            alert(data.message);
        }else{
            alert(data.message);
        }
    });
  });
  //注册
  $('.reg').click(function(e) {
        e.preventDefault();
        var code = $("#code").val();
        check_phone();
        if(code ==''){
            alert('验证码不能为空');
            return false;
        }
        $(this).addClass('layui-btn-disabled');
        $(this).attr('disabled',"true")
        $.post("",{phone:$('#phone').val(),code:code},function(data){
            console.log(data);
            if(data.status == 1){
                alert(data.message);
                //location.href = "{:url('/index/index')}";
                location.href = "{'user/authentication')}";
            }else{
                alert(data.message);
            }
        });
    });



</script>


</body>
</html>